package com.logic.landseaserver.service.impl;

import com.logic.common.exception.BusinessException;
import com.logic.landseaserver.common.VariableChar;
import com.logic.landseaserver.service.IWebsiteConfig;
import com.logic.landseaserver.ws.dto.WebsiteConfigDTO;
import com.logic.landseaserver.ws.request.WebsiteConfig;
import com.logic.system.domain.Configuration;
import com.logic.system.domain.SystemFile;
import com.logic.system.service.IConfigurationService;
import com.logic.system.service.IFileService;
import com.logic.system.ws.dto.FileConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: quintina
 * @Date: 2017/8/9
 * @Description: 关于朗诗寓PC页面配置
 */
@Service
public class WebsiteConfigImpl implements IWebsiteConfig {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private IFileService fileService;

    @Autowired
    private IConfigurationService configurationService;

    @Override
    public void edit(WebsiteConfig config) throws BusinessException {
        //官网地址
        if (!StringUtils.isEmpty(config.getAddress())) {
            updateOrInsertConfig("WEBSITE_CONFIG_ADDRESS", config.getAddress(), VariableChar.WEBSITE_CONFIG_ADDRESS_DESC);
        }
        //邮箱
        if (!StringUtils.isEmpty(config.getMail())) {
            updateOrInsertConfig("WEBSITE_CONFIG_MAIL", config.getMail(), VariableChar.WEBSITE_CONFIG_MAIL_DESC);
        }
        //phone
        if (!StringUtils.isEmpty(config.getPhone())) {
            updateOrInsertConfig("WEBSITE_CONFIG_PHONE", config.getPhone(), VariableChar.WEBSITE_CONFIG_PHONE_DESC);
        }
        //tax
        if (!StringUtils.isEmpty(config.getTax())) {
            updateOrInsertConfig("WEBSITE_CONFIG_TAX", config.getTax(), VariableChar.WEBSITE_CONFIG_TAX_DESC);
        }
        //微信
        if (config.getWxCode() != null) {
            updateOrInsertConfig("WEBSITE_CONFIG_WXCODE", String.valueOf(config.getWxCode()), VariableChar.WEBSITE_CONFIG_WXCODE_DESC);
        }
        //微信
        if (config.getSinaId() != null) {
            updateOrInsertConfig("WEBSITE_CONFIG_SINAID", String.valueOf(config.getSinaId()), VariableChar.WEBSITE_CONFIG_SINAID_DESC);
        }
    }

    @Override
    public WebsiteConfigDTO get() throws BusinessException {
        WebsiteConfigDTO dto = new WebsiteConfigDTO();
        dto.setAddress(configurationService.getStringValue("WEBSITE_CONFIG_ADDRESS"));
        dto.setTax(configurationService.getStringValue("WEBSITE_CONFIG_TAX"));
        dto.setMail(configurationService.getStringValue("WEBSITE_CONFIG_MAIL"));
        dto.setPhone(configurationService.getStringValue("WEBSITE_CONFIG_PHONE"));

        Integer wxcodeId = configurationService.getIntegerValue("WEBSITE_CONFIG_WXCODE");
        if (null != wxcodeId) {
            dto.setWxCode(FileConverter.fromFileToDTO((SystemFile) this.fileService.get(wxcodeId)));
        }
        Integer sinaId = configurationService.getIntegerValue("WEBSITE_CONFIG_SINAID");
        if (null != sinaId) {
            dto.setSinaCode(FileConverter.fromFileToDTO((SystemFile) this.fileService.get(sinaId)));
        }
        return dto;
    }


    private void updateOrInsertConfig(String key, String value, String description) throws BusinessException {
        Configuration configuration = new Configuration();
        configuration.setKeyName(key);
        configuration.setValue(value);
        String address = configurationService.getStringValue(key);
        if (address == null) {
            configuration.setDescription(description);
            configurationService.insert(configuration);
        } else {
            configurationService.update(configuration);
        }
    }


}
